package com.example.test01;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SpinnerItemActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    //定义下拉列表需要显示的文本图标
    private static final int[] iconArray = {
            R.drawable.xx,R.drawable.ic_launcher_background,
            R.drawable.xx,R.drawable.ic_launcher_background,
            R.drawable.xx,R.drawable.ic_launcher_background
    };

    //定义下拉列表需要显示的行星名称数组
    private static final String[] starArray = {"水星","金星","地球","火星","木星","土星"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_item);


        List<Map<String, Object>> list = new ArrayList<>();
        //遍历两个数组
        for(int i=0 ; i<iconArray.length;i++){
            Map<String,Object> item = new HashMap<>();
            item.put("icon",iconArray[i]);
            item.put("name",starArray[i]);
            list.add(item);
        }


        //声明一个下拉列表的简单适配器，其中指定了图标与文本两组数据
        SimpleAdapter startAdapter = new SimpleAdapter(this,list,R.layout.item_simple,
                new String[]{"icon","name"},
                new int[]{R.id.iv_icon,R.id.tv_name});



        Spinner sp_icon = findViewById(R.id.sp_icon);
        sp_icon.setAdapter(startAdapter);
        sp_icon.setSelection(0);
        sp_icon.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
